<template>
  <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
        <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="hfocus" />
      <div v-if="showTree" class="tree-box">
        <el-tree :data="list" :props="{label:'name'}" @node-click="handleNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// 导入枚举
import EmployeesEnum from '@/constant/employees'
import { getDepartments } from '@/api/department'

import { arr2tree } from '@/utils'
import { addEmployee } from '@/api/employees'
export default {
  name: 'EmpDialog',
  // 导入组件名
  components: {},
  // 父传子,接收变量名
  props: {},
  data() {
    // 这里存放数据
    return {
      showTree: false, // 控制部门树的显示隐藏
      hireType: EmployeesEnum.hireType, // 枚举 正式 | 非正式
      list: [], // 部门列表
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  // 计算属性 类似于 data 概念
  computed: {},
  // 监听器 监控 data 中的数据变化
  watch: {},
  created() {

  },
  // 方法集合
  methods: {
    // 获取部门列表
    async loadDepartments() {
      try {
        const { data: res } = await getDepartments()
        // 对原始数据进行一个筛的选
        res.depts.shift()
        // console.log(res)
        this.list = arr2tree(res.depts)
      } catch (error) {
        return new Error(error)
      }
    },
    // 新增员工
    async doAdd() {
      try {
        console.log(this.formData)
        const res = await addEmployee(this.formData)
        console.log('addEmployee', res)
        // 关闭弹层
        this.$emit('success')
      } catch (err) {
        console.log('addEmployee', err)
      }
    },
    // 部门获取焦点事件
    hfocus() {
      this.loadDepartments()
      this.showTree = true
    },
    // 重置表单
    resetForm() {
      this.$refs.addForm.resetFields()
    },
    // 点击取消
    hCancel() {
      // console.log(this.formData.formOfEmployment)
      this.$emit('close')
    },
    // 点击确认
    hSubmit() {
      // 兜底校验
      this.$refs.addForm.validate(valid => {
        if (!valid) return
        this.doAdd()
        // 关闭弹框
        // this.$emit('close')
      })
    },
    // tree组件方法
    handleNodeClick(data) {
      console.log(data)
      // 需求 有子节点时 不填充到表单中
      if (data.children.length !== 0) return
      // 没有子节点 填充到表单中
      this.formData.departmentName = data.name
      // 隐藏tree组件的盒子
      this.showTree = false
    }
  }
}
</script>

<style scoped>
</style>
